package com.yxhl.zoume.core.tripsmgmt.ui.fragment.dialog;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.IRefundTicketPresenter;
import com.yxhl.zoume.data.http.model.trips.TripPassenger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class CancelTicketDialogFragment extends DialogFragment {

    private static final String TAG = "CancelTicketDialog";

    private static final String ARGUMENT_KEY_ORDER_ID = "ARGUMENT_KEY_ORDER_ID";
    private static final String ARGUMENT_KEY_REFUND_PASSENGER = "ARGUMENT_KEY_REFUND_PASSENGER";

    @BindView(R.id.tv_fragment_cancel_ticket_title)
    TextView titleTv;
    @BindView(R.id.tv_fragment_cancel_ticket_dialog_content)
    TextView contentMsgTv;
    @BindView(R.id.tv_fragment_cancel_ticket_dialog_confirm)
    TextView confirmTv;
    @BindView(R.id.tv_fragment_cancel_ticket_dialog_cancel)
    TextView cancelTv;

    //arguments
    private IRefundTicketPresenter mIRefundTicketPresenter;

    private String mOrderId;
    private TripPassenger mTripPassenger;

    private boolean showErrorMessage = false;

    public CancelTicketDialogFragment() {
        // Required empty public constructor
    }

    public static CancelTicketDialogFragment newInstance(String orderId, TripPassenger tripPassenger) {
        CancelTicketDialogFragment fragment = new CancelTicketDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARGUMENT_KEY_ORDER_ID, orderId);
        args.putSerializable(ARGUMENT_KEY_REFUND_PASSENGER, tripPassenger);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0);

        Bundle arguments = getArguments();
        if (arguments != null) {
            mOrderId = arguments.getString(ARGUMENT_KEY_ORDER_ID);
            mTripPassenger = (TripPassenger) arguments.getSerializable(ARGUMENT_KEY_REFUND_PASSENGER);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cancel_ticket_dialog, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @OnClick(R.id.tv_fragment_cancel_ticket_dialog_confirm)
    public void onConfirmClick(View view) {
        // 发起网络请求
        if (showErrorMessage) {
            dismiss();
        } else {
            if (mIRefundTicketPresenter != null) {
                if (!TextUtils.isEmpty(mOrderId) && mTripPassenger != null) {
                    mIRefundTicketPresenter.refundTicket(mOrderId, mTripPassenger);
                }
            }
        }
    }

    @OnClick(R.id.tv_fragment_cancel_ticket_dialog_cancel)
    public void onCancelClick(View view) {
        dismiss();
    }

    public void setPresenter(IRefundTicketPresenter iRefundTicketPresenter) {
        this.mIRefundTicketPresenter = iRefundTicketPresenter;
    }

    public void changeContent(String newContent) {
        if (!TextUtils.isEmpty(newContent)) {
            titleTv.setText("提示");
            contentMsgTv.setText(newContent);
            cancelTv.setVisibility(View.INVISIBLE);
            showErrorMessage = true;
        }
    }

}
